﻿using System.ComponentModel;
using System.Windows;
using Gavel.Common.Utilities.Const;
using Gavel.Common.Utilities.Extension;

namespace Gavel.ModLink.ViewModel;

/// <summary>
/// 地址配置视图模型
/// </summary>
public class AddressViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// 属性修改事件
    /// </summary>
    public event PropertyChangedEventHandler? PropertyChanged;

    /// <summary>
    /// 主机地址
    /// </summary>
    private string _host = "127.0.0.1";

    /// <summary>
    /// 主机地址
    /// </summary>
    public string Host
    {
        get => _host;
        set
        {
            _host = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Host)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnableConnect)));
        }
    }
    
    /// <summary>
    /// 端口号
    /// </summary>
    private ushort _port = 502;

    /// <summary>
    /// 端口号
    /// </summary>
    public ushort Port
    {
        get => _port;
        set
        {
            _port = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Port)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnableConnect)));
        }
    }

    /// <summary>
    /// 套接字状态
    /// </summary>
    private SocketStatus _status;

    /// <summary>
    /// 套接字状态
    /// </summary>
    public SocketStatus Status
    {
        get => _status;
        set
        {
            _status = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Title)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InputReadOnly)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnableConnect)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ConnectButtonContent)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ConnectButtonVisibility)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DisconnectButtonVisibility)));
        }
    }

    /// <summary>
    /// 视图标题
    /// </summary>
    public string Title => Status switch
    {
        SocketStatus.Connected => "地址配置（已连接）",
        SocketStatus.Connecting => "地址配置（连接中）",
        _ => "地址配置（未连接）"
    };

    /// <summary>
    /// 输入框只读
    /// </summary>
    public bool InputReadOnly => Status != SocketStatus.Normal;

    /// <summary>
    /// 启用连接按钮
    /// </summary>
    public bool EnableConnect => Host.IsIpv4() && Port > 0 && Status == SocketStatus.Normal;
    
    /// <summary>
    /// 连接按钮内容
    /// </summary>
    public string ConnectButtonContent => Status == SocketStatus.Connecting ? "连接中" : "连接";
    
    /// <summary>
    /// 链接按钮可见性
    /// </summary>
    public Visibility ConnectButtonVisibility => Status == SocketStatus.Connected ? Visibility.Collapsed : Visibility.Visible;
    
    /// <summary>
    /// 断开连接按钮可见性
    /// </summary>
    public Visibility DisconnectButtonVisibility => Status == SocketStatus.Connected ? Visibility.Visible : Visibility.Collapsed;
}